<?php
namespace app\admin\controller;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\Session;
use think\facade\Validate;

class Login extends BaseController
{
    //初始化登录验证session
    protected function initialize(){}
    //登录
    public function login()
    {
        $free= Request::param('change','');

        //如果有免登陆的值的话  渲染免登录页
        if (\cookie('free') && !$free){
            $token=explode('-',\cookie('free'));
            $user=\app\admin\model\Admin::find($token[0]);
            $tokens=md5(md5($user['name']).md5($user['pwd']).'token');
            if ($token[1]==$tokens){
                return view('login/free',$user->toArray());
            }
        }
        //30天免登录
        if (Request::isPost()){
            //1.接参数
            $post=Request::post();
            $time=\cache($post['name'].'_time');
            //限定一天登录
            if (cache($post['name'])>=5 && time()-$time<=86400){
                $this->error('禁止当前登录');
            }else{
                //登录一天之后清除缓存
                Cache::delete($post['name']);
                Cache::delete($post['name'].'_time');
            }
            //判断是否要验证码
            if (cache($post['name'])>3 && time()-$time<=3600){
                $scene='iscode';
            }else{
                $scene='noCode';
            }
            //验证登录的数据
            try{
                //登录是使用验证器验证
                validate(\app\admin\validate\Login::class)->scene($scene)->check($post);
            }catch (ValidateException $e){
                $this->error($e->getMessage());
            }
            //查询的账号
            $user=\app\admin\model\Admin::where('name','=',$post['name'])->where('pwd','=',$post['password'])->find();
            //成功失败跳转
            if ($user){
                Cache::delete($post['name']);
                session('userInfo',$user);
                if ($post['free']){
                    $token=md5(md5($user['name']).md5($user['pwd']).'token');
                    Cookie::set('free',$user['id'].'-'.$token,86400*30);
                }
                $this->success('登陆成功',url('Index/index'));
            }else{
                Cache::inc($post['name']);
                if (!cache($post['name'].'_time')){
                    \cache($post['name'].'_time',time());
                }
                $this->error('登陆失败',url('Login/login',['name'=>$post['name']]));
            }
        }else{
            return view();
        }
    }
    //免登录验证
    public function loginfree()
    {
        if (\cookie('free')){
            $token=explode('-',\cookie('free'));
            $user=\app\admin\model\Admin::find($token[0]);
            if ($user){
                session('userInfo',$user);
                $this->success('登陆成功',url('Index/index'));
            }else{
                $this->error('登陆失败',url('Login/login'));
            }
        }
    }
    //验证验证码
    public function ajaxCode()
    {
       $code=Request::param('code');
       //一样的话返回一个值，不一样返回一个值
       if (captcha_check($code,false)){
           return json(['code'=>3,'msg'=>'ok']);
       }else{
           return json(['code'=>0,'msg'=>'验证码错误']);
       }
    }
    //验证用户名
    public function ajaxName()
    {
        $name=Request::param('name');
        $time=\cache($name.'_time');
        if (cache($name)>=2 && time()-$time>=86400){
            Cache::delete($name);
            Cache::delete($name.'_time');
        }
        if (cache($name)<=3 && time()-$time<=3600){
            return json(['code'=>1,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'msg'=>'开启验证码']);
        }
    }
    //退出登录
    public function loginOut()
    {
        //清除所有的session缓存   并返回登录页
        Session::clear();
        $this->success('退出成功',url('Login/login'));
    }
}